//
//  DatePicker.m
//  homeland
//
//  Created by SunShine on 13-10-30.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "DatePicker.h"
@interface DatePicker ()
@property (strong, nonatomic) UIDatePicker *picker;
@end

@implementation DatePicker

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        //初始化位置在屏幕外面
        self.frame = CGRectMake(0.0, SCREEN_HEIGHT, 320.0, 216.0+30.0);
        //绘制工具条
        UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, 0.0, 320, 30)];
        UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleBordered target:self action:@selector(doneAction:)];
        UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelAction:)];
        toolBar.items = [NSArray arrayWithObjects:cancelButton,space, doneButton, nil];
        toolBar.barStyle = UIBarStyleBlackTranslucent;
        [self addSubview:toolBar];
        //绘制picker
        _picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 30.0, 320.0, 216.0)];
        [self addSubview:_picker];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

-(void)initPickerWithMode:(UIDatePickerMode)mode DoneBlock:(void (^)(NSDate *))doneBlock BeginDate:(NSDate *)begin EndDate:(NSDate *)end
{
    chooseDoneBlock = doneBlock;
    _picker.datePickerMode = mode;
    if (begin) {
        _picker.minimumDate = begin;
    }
    if (end) {
        _picker.maximumDate = end;
    }
}


#pragma mark - Show/Hide Methods
- (void)showDatePicker
{
    //将选择器显示在视图最上层
    UIWindow *window = [[[UIApplication sharedApplication] windows]objectAtIndex:0];
    for (id object in window.subviews) {
        if ([object isEqual:self]) {
            [window bringSubviewToFront:self];
        }else{
            [window addSubview:self];
        }
    }
    //向上讲选择器推入视图的显示区域
    [UIView beginAnimations:@"ShowPicker" context:nil];
	[UIView setAnimationDuration:0.35];
	CGRect frame = self.frame;
    frame.origin.y = SCREEN_HEIGHT - frame.size.height;
    self.frame = frame;
	[UIView commitAnimations];
}

- (BOOL)hideDatePicker
{
    if (self.frame.origin.y < SCREEN_HEIGHT) {
        //在屏幕内
        [UIView beginAnimations:@"HidePicker" context:nil];
		[UIView setAnimationDuration:0.35];
        [UIView setAnimationDelegate:self];
		[UIView setAnimationDidStopSelector:@selector(animationDidStop)];
		CGRect frame = self.frame;
		frame.origin.y = SCREEN_HEIGHT;
		self.frame = frame;
        [UIView commitAnimations];
        return YES;
    }
    return NO;
}

-(void)animationDidStop
{
    [self removeFromSuperview];
}
#pragma mark - Button Action
-(void)cancelAction:(id)sender
{
    [self hideDatePicker];
}

-(void)doneAction:(id)sender
{
    //获取选择的结果
    NSDate *result = _picker.date;
    chooseDoneBlock(result);
    [self hideDatePicker];
}
@end
